#include "myhead.h"
#include "server.h"

int c_fd;
void *getmsg()
{
    char buf[100];
    while (1)
    {
        bzero(buf, 100);
        struct sockaddr_in c_addr;
        socklen_t len = sizeof(struct sockaddr_in);
        recvfrom(c_fd, buf, 100, 0, (struct sockaddr *)&c_addr, &len);

        printf("收到消息了 %s \n", buf);
    }
}

int main(int argc, char const *argv[])
{
    c_fd = socket(AF_INET, SOCK_DGRAM, 0);

    char buf[100];

    struct sockaddr_in s_addr;
    s_addr.sin_family = AF_INET;
    s_addr.sin_addr.s_addr = inet_addr("192.168.6.3");
    s_addr.sin_port = htons(3000);

    pthread_t tid;
    pthread_create(&tid, NULL, getmsg, &c_fd);
    // 自动请求发起连接
    sendto(c_fd, CONNECT, strlen(CONNECT), 0, (struct sockaddr *)&s_addr, sizeof(s_addr));

    while (1)
    {
        printf("请输入要发送的数据: \n");
        scanf("%s", buf);
        sendto(c_fd, buf, strlen(buf), 0, (struct sockaddr *)&s_addr, sizeof(s_addr));
    }

    return 0;
}
